package it.datagraf.formazione.hellostruts.struts;

import it.datagraf.formazione.hellostruts.entity.CurriculumVitae;
import it.datagraf.formazione.hellostruts.form.BeanCurriculumForm;
import it.datagraf.formazione.hellostruts.form.InsertCommentForm;
import it.datagraf.formazione.hellostruts.service.CVServiceInteface;
import it.datagraf.formazione.hellostruts.service.CVserviceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class OwnerEditCvAction extends Action {

	public static Log log = LogFactory.getLog(OwnerEditCvAction.class);

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		BeanCurriculumForm ef = (BeanCurriculumForm) form;
		
		log.info("Sono in OwnerEditCvAction - aggiorno nel db il curriculum di "+
		ef.getCurriculumVitae().getNome()+" "+ef.getCurriculumVitae().getCognome());
		
		CurriculumVitae cv = ef.getCurriculumVitae();

		WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
		CVServiceInteface serv= (CVServiceInteface) ctx.getBean("CVservice");
		
		
		cv.setId(serv.findId(request.getRemoteUser()));
		
		cv.setNome(cv.getNome());
		cv.setCognome(cv.getCognome());
		cv.setSesso(cv.getSesso());
		cv.setOwner(request.getRemoteUser());
		cv.setNote(cv.getNote());
		cv.setFile(cv.getFile());
		cv.setEta(cv.getEta());
	 
 
		serv.update(cv);
		
		request.setAttribute("cv", cv);
		

		return mapping.findForward("success");
	}
}